package com.dji.uxsdkdemo;

import android.util.Log;

import com.dji.uxsdkdemo.modle.*;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;
import java.net.URISyntaxException;

public class SocketClient extends WebSocketClient {
//单例
    // 使用静态变量记录唯一的实例
    private static SocketClient SocketClientInstance;
    private SocketClient(URI serverUri){
        super(serverUri);
    }
    public static synchronized SocketClient getSocketClient(URI serverUri){
        if(SocketClientInstance == null) {
            SocketClientInstance = new SocketClient(serverUri);
        }
        // 返回Sensor唯一实例
        return SocketClientInstance;
    }
    public static synchronized SocketClient getSocketClient(){
        if(SocketClientInstance == null) {
            return null;
        }
        // 返回Sensor唯一实例
        return SocketClientInstance;
    }


    private static final String TAG = "SocketClient";

    public DJIWebSocketDateRecive DJIWebSocketDateRecive;
    public DJIWebSocketDateSet DJIWebSocketDateSet;
    /**
     * 此方法为了直接运行测试用例，实际使用自行此类重写
     * @param args
     * @throws URISyntaxException
     */
    public static void main(String[] args) throws URISyntaxException {
        WebSocketClient client = new SocketClient(new URI(Utils.ws));
        client.connect();
    }


    @Override
    public void onOpen(ServerHandshake handshakedata) {
        Log.d(TAG, "myWsManager-----onOpen");
        Log.e("onOpen", "服务器连接成功");
    }

    @Override
    public void onMessage(String message) {
        System.out.println(message);
        //在这里接收和处理收到的ws数据
        try{
            JSONObject jsonObject = new JSONObject(message);
            JSONObject data = jsonObject.getJSONObject("data");
            DJIWebSocketDateRecive  = new DJIWebSocketDateRecive();
            DJIWebSocketDateRecive. createDate = data.getString("createDate");
            DJIWebSocketDateRecive. creator = data.getString("creator");
            DJIWebSocketDateRecive. deviceId = data.getString("deviceId");
            DJIWebSocketDateRecive. id = data.getString("id");
            DJIWebSocketDateRecive. latitude = data.getString("latitude");
            DJIWebSocketDateRecive. longitude = data.getString("longitude");
            DJIWebSocketDateRecive. mapId = data.getString("mapId");
            DJIWebSocketDateRecive. taskName = data.getString("taskName");
            DJIWebSocketDateRecive. taskRemark = data.getString("taskRemark");
            DJIWebSocketDateRecive. type = data.getString("type");
            DJIWebSocketDateRecive. updateDate = data.getString("updateDate");
            DJIWebSocketDateRecive. updater = data.getString("updater");
            DJIWebSocketDateRecive. warningName = data.getString("warningName");
        }catch (Exception e){e.printStackTrace();}
    }

    public String setJson(DJIWebSocketDateSet dateSet){
        if(DJIWebSocketDateRecive==null) {
            Log.d("setJson :", "没有接收到信息!");
            return "";
        }
        try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("latitude", dateSet.latitude);
        jsonObject.put("longitude", dateSet.longitude);
        jsonObject.put("taskId", DJIWebSocketDateRecive.id);
        jsonObject.put("wlatitude", DJIWebSocketDateRecive.latitude);
        jsonObject.put("wlongitude", DJIWebSocketDateRecive.longitude);
        jsonObject.put("mHomeLat", dateSet.mHomeLat);
        jsonObject.put("mHomeLng", dateSet.mHomeLng);
        jsonObject.put("VelocityX", dateSet.VelocityX);
        jsonObject.put("VelocityY", dateSet.VelocityY);
        jsonObject.put("VelocityZ", dateSet.VelocityZ);
        jsonObject.put("FlightMode", dateSet.FlightMode);
        jsonObject.put("isFlying", dateSet.isFlying);
        jsonObject.put("wlongitude", dateSet.FlightTime);

        String strResult = jsonObject.toString();
        Log.d("setJson :", strResult);
        return strResult;
//        send(strResult);
        } catch (JSONException ex) {
            // 键为null或使用json不支持的数字格式(NaN, infinities)
            throw new RuntimeException(ex);
        }
    }
    @Override
    public void onClose(int code, String reason, boolean remote) {
        System.out.println( "Connection closed by " + ( remote ? "remote peer" : "us" ) );
    }

    @Override
    public void onError(Exception ex) {
        ex.printStackTrace();
    }

}
